package android.framework.media;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.framework.app.base.BaseActivity;
import android.framework.global.Constants;
import android.framework.util.UIs;
import android.framework.util.Util;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import android.framework.R;

public abstract class BasePlayActivity extends BaseActivity {

	
	protected int play_type = Constants.PLAY_DEFAULT; // 1--octo ,2--hls

	protected View brightnessLayout;
	protected ProgressBar brightnessSeekbar;
	protected long currentTime;
	protected GestureLayout gestureLayout;
	protected RelativeLayout guideLayout;
	protected boolean isRegStateReceiver = false;
	protected View lockBar;

	protected ImageView play_loading_battery = null;
	protected ImageView play_loading_net = null;
	protected TextView play_loading_time = null;
	protected View progressLayout;
	protected TextView progressTextView;
	protected TextView totalTextView;
	protected View volumeLayout;
	protected ProgressBar volumeSeekbar;

	public abstract void doBack();

	public abstract PlayController getPlayController();

	protected BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context paramContext, Intent paramIntent) {
			if ("android.intent.action.TIME_TICK".equals(paramIntent
					.getAction()))
				// UiUtils.changeTimeState(BasePlayActivity.this.play_loading_time);
				;
			else if ("android.net.conn.CONNECTIVITY_CHANGE".equals(paramIntent
					.getAction()))
				// UiUtils.changeNetState(BasePlayActivity.this,
				// BasePlayActivity.this.play_loading_net);
				;
			else if (!"android.intent.action.BATTERY_CHANGED"
					.equals(paramIntent.getAction())) {
				int i = paramIntent.getIntExtra("status", 1);
				int j = paramIntent.getExtras().getInt("level", 0);
				int k = paramIntent.getExtras().getInt("scale", 100);
				// UiUtils.changeBatteryState(i, j * 100 / k,
				// BasePlayActivity.this.play_loading_battery);
			}
		}
	};

	protected void registerReceiver() {
		if (this.isRegStateReceiver)
			return;
		IntentFilter localIntentFilter = new IntentFilter();
		localIntentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
		localIntentFilter.addAction("android.intent.action.TIME_TICK");
		localIntentFilter.addAction("android.intent.action.BATTERY_CHANGED");
		registerReceiver(this.mBroadcastReceiver, localIntentFilter);
		this.isRegStateReceiver = true;
	}

	protected void unregisterReceiver() {
		if (!this.isRegStateReceiver)
			return;
		unregisterReceiver(this.mBroadcastReceiver);
		this.isRegStateReceiver = false;
	}

	public void LockRegulate(boolean paramBoolean) {
		View localView;
		if (this.lockBar != null) {
			localView = this.lockBar;
			if (!paramBoolean)
				return;
			else {
				localView.setVisibility(View.VISIBLE);
			}
		}
	}

	public void brightnessRegulate(int paramInt) {
		if ((this.brightnessLayout != null)
				&& (this.brightnessLayout.getVisibility() != 0))
			this.brightnessLayout.setVisibility(0);
		if (this.brightnessSeekbar == null)
			return;
		this.brightnessSeekbar.setProgress(paramInt);
	}

	public void hideBrightnessLayout() {
		if (this.brightnessLayout == null)
			return;
		this.brightnessLayout.setVisibility(8);
	}

	public void hideGestureNotice() {
		this.guideLayout.setVisibility(8);
		this.guideLayout.setOnClickListener(null);
	}

	public void hideVolumeLayout() {
		if (this.volumeLayout == null)
			return;
		this.volumeLayout.setVisibility(8);
	}

	public void hideprogressLayout() {
		if (this.progressLayout == null)
			return;
		this.progressLayout.setVisibility(8);
	}

	public void initBrightness(int paramInt1, int paramInt2) {
		if (this.brightnessLayout != null)
			this.brightnessLayout.setVisibility(8);
		if (this.brightnessSeekbar == null)
			return;
		this.brightnessSeekbar.setMax(paramInt1);
		this.brightnessSeekbar.setProgress(paramInt2);
	}

	public void initVolume(int paramInt1, int paramInt2) {
		if (this.volumeLayout != null)
			this.volumeLayout.setVisibility(8);
		if (this.volumeSeekbar == null)
			return;
		this.volumeSeekbar.setMax(paramInt1);
		this.volumeSeekbar.setProgress(paramInt2);
	}

	public void initVolumeMax(int paramInt) {
		this.volumeSeekbar.setMax(paramInt);
	}

	public boolean isLock() {
		if (this.gestureLayout != null)
			return this.gestureLayout.isLock();
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		if (Build.VERSION.SDK_INT >= 9) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
		} else {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		setContentView(R.layout.play_main);
		this.guideLayout = ((RelativeLayout) findViewById(R.id.guide_layout));
		this.gestureLayout = (GestureLayout) findViewById(R.id.gesture_layout);
		this.brightnessLayout = findViewById(R.id.brightness_layout);
		this.volumeLayout = findViewById(R.id.volume_layout);
		this.brightnessSeekbar = ((ProgressBar) findViewById(R.id.brightness_verseekbar));
		this.volumeSeekbar = ((ProgressBar) findViewById(R.id.volume_verseekbar));
		this.lockBar = findViewById(R.id.lock);
		this.progressLayout = findViewById(R.id.progress_layout);
		this.progressTextView = ((TextView) findViewById(R.id.progress_text));
		this.totalTextView = ((TextView) findViewById(R.id.total));
		findViewById(R.id.play_loadingview_back).setOnClickListener(
				new BackEvent());
		this.play_loading_net = ((ImageView) findViewById(R.id.play_loading_net));
		this.play_loading_battery = ((ImageView) findViewById(R.id.play_loading_battery));
		this.play_loading_time = ((TextView) findViewById(R.id.play_loading_time));
		// UiUtils.changeTimeState(this.play_loading_time);
		if (PreferencesManager.getInstance().isFirstPlay()) {
			this.guideLayout.setVisibility(View.VISIBLE);
			this.guideLayout.setOnClickListener(new View.OnClickListener() {
				public void onClick(View paramView) {
					BasePlayActivity.this.guideLayout.setVisibility(View.GONE);
					PreferencesManager.getInstance().setFirstPlay(false);
				}
			});
		}
		if (!isRegStateReceiver) {
			registerReceiver();
			this.gestureLayout.initializeData();
			this.guideLayout.setVisibility(View.GONE);
		}
	}

	protected void onDestroy() {
		try {
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			unregisterReceiver();
		} catch (Exception localException) {
		}
		super.onDestroy();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case 4:
			finishActivity();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void finishActivity() {
		if (System.currentTimeMillis() - this.currentTime < 3000L) {
			doBack();
			UIs.cancelNotify();
			finish();
		} else {
			this.currentTime = System.currentTimeMillis();
			UIs.notifyLongNormal(this, R.string.play_back_toast);
		}
	}

	protected void onPause() {
		super.onPause();
	}

	protected void onResume() {
		super.onResume();
	}

	protected void onStart() {
		super.onStart();
	}

	protected void onStop() {
		super.onStop();
	}

	public void progressRegulate(int paramInt1, int paramInt2) {
		if ((this.progressLayout != null)
				&& (this.progressLayout.getVisibility() != View.VISIBLE))
			this.progressLayout.setVisibility(View.VISIBLE);
		if (this.progressTextView != null)
			this.progressTextView.setText(Util.stringForTime(paramInt1));
		if (this.totalTextView == null)
			return;
		this.totalTextView.setText(Util.stringForTime(paramInt2));
	}

	public void showGestureNotice() {
		if (!PreferencesManager.getInstance().isFirstPlay())
			return;
		this.guideLayout.setVisibility(View.VISIBLE);
		this.guideLayout.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				BasePlayActivity.this.guideLayout.setVisibility(View.GONE);
				PreferencesManager.getInstance().setFirstPlay(false);
			}
		});
	}

	public void volumeRegulate(int paramInt) {
		if ((this.volumeLayout != null)
				&& (this.volumeLayout.getVisibility() != View.VISIBLE))
			this.volumeLayout.setVisibility(View.VISIBLE);
		if (this.volumeSeekbar == null)
			return;
		this.volumeSeekbar.setProgress(paramInt);
	}

	private class BackEvent implements View.OnClickListener {
		private BackEvent() {
		}

		public void onClick(View paramView) {
			BasePlayActivity.this.doBack();
		}
	}
}
